
package codeGenerate.front.generater;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import codeGenerate.front.tempalte.TemPathFrontAbstract;
import codeGenerate.front.vo.FieldVo;
import codeGenerate.front.vo.FrontConfigVo;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FrontGenerater {

	private FrontConfigVo configVo;

	/**
	 * @description: 生成代码
	 * @creator: dw.fu
	 * @createDate: 2015年9月26日 
	 * @modifier:
	 * @modifiedDate:
	 * @throws IOException
	 * @throws TemplateException
	 * @throws ClassNotFoundException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public void generateCode() throws IOException, TemplateException, ClassNotFoundException, InstantiationException, IllegalAccessException {
		System.out.println(">>>>>>>>>>>>>>>>>>>[" + configVo.getBeanDescription() + "]生成开始<<<<<<<<<<<<<<<<<<<");
		System.out.println(configVo);

		Map<String, Object> root = new HashMap<String, Object>();
		//定义模板常量
		root.put("config", configVo);
		String simpleBeanName = configVo.getBeanName().substring(0, 1).toLowerCase() + configVo.getBeanName().substring(1);
		//避免手动输入bean类首个字母小写
		root.put("beanName", configVo.getBeanName().substring(0, 1).toUpperCase() + configVo.getBeanName().substring(1));
		root.put("simpleBeanName", simpleBeanName);
		root.put("listSearchFields", configVo.getListSearchFields());
		root.put("listTableFields", configVo.getListTableFields());
		List<FieldVo> formFields = configVo.getFormFields();
		root.put("formFields", formFields);
		root.put("ctx", "${ctx}");
		root.put("fieldId", formFields.get(0).getName());
		root.put("dollar", "$");

		List<FieldVo> validateList = new ArrayList<FieldVo>();
		List<FieldVo> fileFieldList = new ArrayList<FieldVo>();
		for (FieldVo fieldVo : formFields) {
			if (StringUtils.isNotBlank(fieldVo.getValidate())) {
				validateList.add(fieldVo);
			}
			if ("file".equals(fieldVo.getType())) {
				fileFieldList.add(fieldVo);
			}
		}
		root.put("validateList", validateList);
		root.put("fileFieldList", fileFieldList);
		boolean containFile = false;
		if (fileFieldList.size() > 0) {
			containFile = true;
		}
		root.put("containFile", containFile);

		Configuration cfg = new Configuration();
		cfg.setClassicCompatible(true);
		cfg.setDirectoryForTemplateLoading(new File(configVo.getTemplateDirectory()));
		cfg.setObjectWrapper(new DefaultObjectWrapper());
		//根据模版路径生成相应的代码
		Class<?> tempateClass = Class.forName(configVo.getTemplate());
		TemPathFrontAbstract temPathAbstract = (TemPathFrontAbstract) tempateClass.newInstance();
		Map<String, String> tplFileMap = temPathAbstract.getPathMap(configVo);
		for (Map.Entry<String, String> entry : tplFileMap.entrySet()) {
			String tempalteFile = entry.getKey();
			String generateFile = entry.getValue();
			Template temp = cfg.getTemplate(tempalteFile);
			File file = new File(generateFile);
			if (!file.exists()) {
				file.getParentFile().mkdirs();
				file.createNewFile();
			}
			OutputStream writer = new FileOutputStream(new File(generateFile));
			Writer out = new OutputStreamWriter(writer);
			temp.process(root, out);
			writer.flush();
			writer.close();
			System.out.println("模板:" + tempalteFile + "==>>生成文件==>>" + generateFile);
		}

		System.out.println(">>>>>>>>>>>>>>>>>>>表名[" + configVo.getBeanDescription() + "]生成结束<<<<<<<<<<<<<<<<<<<");
		System.out.println("\n");
	}

	public FrontConfigVo getConfigVo() {
		return configVo;
	}

	public void setConfigVo(FrontConfigVo configVo) {
		this.configVo = configVo;
	}

}
